package commandes;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import bdd.DBS;
import beans.Proprietaire;

public class CommandeMajProprietaire implements Commande {
	  private String next;

	  public CommandeMajProprietaire(String next) {
	        this.next = next;
	  }

	public String execute(HttpServletRequest req) throws Exception {
		HttpSession session = req.getSession(true);
		if(session.getAttribute("droit").equals("administrateur")) {
			DBS db = DBS.getInstance();
			
			if(req.getParameter("suppr")!=null){
				db.getDB_Proprietaire().deleteProprietaire(Integer.parseInt(req.getParameter("suppr")));
			}
			
		
			
			if(req.getParameter("mod")!=null){
				req.setAttribute("mod",req.getParameter("mod"));
			}else{
				req.setAttribute("mod",0);
			}
			
			String nom=req.getParameter("nom");
			String prenom=req.getParameter("prenom");
			String adresse=req.getParameter("adresse");
			String tel=req.getParameter("tel");
			String id=req.getParameter("id");
			
			if(nom != null && prenom !=null && adresse != null && tel!=null){
					Proprietaire p=new Proprietaire(Integer.parseInt(id), nom, prenom, adresse, tel);
					db.getDB_Proprietaire().updateProprietaire(p);		
			}else if(nom == null || prenom == null || adresse == null || tel!=null){
					req.setAttribute("erreur", true);
			}
				
			List<Proprietaire> props = db.getDB_Proprietaire().getProprietaires();
			req.setAttribute("proprietaires", props);
			return next;
		} else {
			return "index.jsp";
		}
	}

}
